package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargePayConfig;
import com.kehutong.charge.service.ChargePayConfigService;
import com.kehutong.charge.util.enums.CommonEnum;
import com.kehutong.common.DeptService;
import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.util.EnumUtil;

import java.util.List;

/**
 * @program: kht
 * @description: 支付配置
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/payment")
public class ChargePayController {

    private static final Logger logger = LoggerFactory.getLogger(ChargePayController.class);

    @Inject
    DeptService deptService;

    @Inject
    ChargePayConfigService chargePayConfigService;

    @ReqMapping("/page")
    @HasPermission("pay:charge:payment:view")
    public Object page(JSONObject jsonObject) {
        return chargePayConfigService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    @HasPermission("pay:charge:payment:view")
    public Object list(JSONObject jsonObject) {
        return chargePayConfigService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    @HasPermission("pay:charge:payment:edit")
    @ReqMapping("/save")
    public Object save(ChargePayConfig entity, Token token,JSONObject jsonObject) throws Exception {
        entity.setCreateName(token.getUsername());
        List<String> departList = token.getDepartments();
        if(null != departList && departList.size() > 0){
            logger.error("用户{}数据异常，部门为空！",token.getUsername());

            DeptDTO deptDTO = deptService.getDept(departList.get(0));
            entity.setDepartmentNo(deptDTO.getDepartmentNo());
            entity.setDepartmentNos(deptDTO.getDepartmentNos());
            entity.setDepartmentName(deptDTO.getDepartmentName());
        }
        entity.setPayMethod(CommonEnum.COM_PAY_WECHAT);
        return chargePayConfigService.save(entity);
    }

    @Bean(copy=true)
    @HasPermission("pay:charge:payment:edit")
    @ReqMapping("/update")
    public Object update(ChargePayConfig entity,JSONObject jsonObject) throws Exception {
        entity.setPayMethod(CommonEnum.COM_PAY_WECHAT);
        return chargePayConfigService.update(entity);
    }

    @HasPermission("pay:charge:payment:edit")
    @ReqMapping("/delete")
    public Object delete(ChargePayConfig entity) {

        return chargePayConfigService.deleteById(entity.getId());
    }

    @ReqMapping("/get")
    public Object get(ChargePayConfig entity) {

        return Result.success(entity);
    }

    @ReqMapping("/setStatus")
    @HasPermission("pay:charge:payment:edit")
    public Object setStatus(ChargePayConfig entity) throws Exception {
        entity.setStatus(!entity.isStatus());
        chargePayConfigService.update(entity);
        return Result.success();
    }

}
